package com.buho29.rcmanager.bd;

import java.util.Date;

import android.content.ContentValues;
import android.database.Cursor;

public class TableAlarm extends BaseActiveRecord  {

	public static final String SQL_TABLE_NAME = "alarm";
	
	public static final String SQL_CREATE_TABLE = "create table " + SQL_TABLE_NAME + " (" +
				"_id INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT," +
			    "model_id INTEGER NOT NULL," +
	    		"name VARCHAR(100) NOT NULL," +
	    		"init INTEGER NOT NULL, " +
			    "step INTEGER NOT NULL," +
	    		"type INTEGER NOT NULL " +
		    ");";
	
	public static final String COL_MODEL_ID = "model_id";
	public static final String COL_INIT = "init";
	public static final String COL_NAME = "name";
	public static final String COL_STEP = "step";
	public static final String COL_TYPE = "type";
	
    public static final int TYPE_TIME = 0;
    public static final int TYPE_DATE = 1;
    
    public static final int STEP_TIME = 60*60;//min *60
    public static final int STEP_DATE = 60000*60*24; // min *60*24
	
	public static final String[] COLUMNS = new String[] { 
		COL_ROW_ID, COL_MODEL_ID,COL_INIT , COL_NAME, COL_STEP ,COL_TYPE
	};
	
	
	private long mModelId;
	public long getModelId() {return mModelId;}
	public void setModelId(long modelId) {mModelId = modelId;}

	private String mName;
	public String getName() {return mName;}
	public void setName(String name) {mName = name;}
		
	private long mInit;
	public long getInit() {return mInit;}
	private void setInit(long init) {mInit = init;}
	
	private long mStep;
	public long getStep() {return mStep;}
	public void setStep(long step) {mStep = step;}
	
	private int mType;
	public int getType() {return mType;}
	public void setType(int cause) {mType = cause;}

	@Override
	public Boolean load() {
		Cursor cursor = mDb.query(true, SQL_TABLE_NAME,COLUMNS,
				COL_ROW_ID + "=" + mId,
				null, null,null, null, null);
		if (cursor != null && cursor.moveToFirst()) {
			loadCursor(cursor);
			cursor.close();
			return true;
		}
		return false;
	}
	public void loadCursor(Cursor cursor) {
		
		setId(cursor.getLong(cursor.getColumnIndex(COL_ROW_ID)));
		setModelId(cursor.getLong(cursor.getColumnIndex(COL_MODEL_ID)));
		setName(cursor.getString(cursor.getColumnIndex(COL_NAME)));
		setInit(cursor.getLong(cursor.getColumnIndex(COL_INIT)));
		setStep(cursor.getLong(cursor.getColumnIndex(COL_STEP)));
		setType(cursor.getInt(cursor.getColumnIndex(COL_TYPE)));
		
	}

	@Override
	public long save() {
		ContentValues values = new ContentValues();
		values.put(COL_MODEL_ID,mModelId);
		values.put(COL_NAME,mName);
		values.put(COL_INIT,mInit);
		values.put(COL_STEP,mStep);
		values.put(COL_TYPE,mType);
		
		if (mId < 1) {
			return mDb.insert(SQL_TABLE_NAME, null, values);
		} else {
			return mDb.update(SQL_TABLE_NAME, values, COL_ROW_ID      + "=" + mId, null);
		}  
	}
	
	@Override
	public boolean delete() {
		return mDb.delete(SQL_TABLE_NAME, COL_ROW_ID + "=" + mId,     null) > 0;
	}
	
	@Override
	public Cursor retrieveAll() {
		return mDb.query(SQL_TABLE_NAME, COLUMNS, null, null,
				null, null, null);
	}
	
	public Cursor fetchModel(long modelId) {
		return  mDb.query(SQL_TABLE_NAME, COLUMNS,COL_MODEL_ID+ "=" + modelId,null,
				null, null, COL_TYPE);
	}
	
	public void reset(){
		if(mType == TYPE_DATE)
			mInit = new Date().getTime();
		else if(mType == TYPE_TIME){
			TableStatistic ts = new TableStatistic();
			ts.setSQLiteDatabase(mDb);
			ts.loadModel(mModelId);
			mInit = ts.getTotal();
		}
	}
	
	public long getTimeLeft() {
		if(mType == TYPE_DATE){
			return (mInit+mStep*STEP_DATE-new Date().getTime())/1000;
		}else if(mType == TYPE_TIME){
			TableStatistic ts = new TableStatistic();
			ts.setSQLiteDatabase(mDb);
			ts.loadModel(mModelId);
			
			return mInit+mStep*STEP_TIME-ts.getTotal();
		}
		return -1;
	}
	
	public boolean deletemodel(long modelID) {
		return mDb.delete(SQL_TABLE_NAME, COL_MODEL_ID + "=" + modelID,     null) > 0;
	}
}
